package com.java.laixiangdong.library.dao;

import com.java.laixiangdong.library.bean.Book;
import com.java.laixiangdong.library.bean.User;
import com.java.laixiangdong.library.view.View;

import java.io.*;
import java.util.*;

public class Dao {
    private static HashSet<Book> books = new HashSet<>();
    View view = new View();

    public boolean login(String name, String password) {
        if (User.NAME.equals(name) && User.getPassword().equals(password)) {
            System.out.println("登陆成功");
            return true;
        } else {
            System.out.println("登陆失败");
            return login(view.inputLogin(), view.inputPassword());
        }
    }

    public void add(String name, String info, Date publishTime, int price) {
        Book book = new Book(name, info, publishTime, price);
        boolean add = books.add(book);
        if (add)
            System.out.println("添加成功");
        else
            System.out.println("已有此图书，添加失败");
    }

    public void search(String name) {
        int count = 0;
        for (Book b : books) {
            if (b.getName().contains(name)) {
                System.out.println(b.toString());
                count++;
            }
        }
        if (count == 0) {
            System.out.println("没有查到相关书籍");
        }
    }

    public void showAll(int i) {
        Book.setSortFlag(i);
        List bookList = Arrays.asList(books.toArray());
        Collections.sort(bookList);
        int count = 0;
        for (Object o : bookList) {
            Book b = (Book) o;
            System.out.println(b.toString());
        }
        if (count == 0) {
            System.out.println("当前没有任何图书");
        }
    }

    public void change(String name) {
        Book book = null;
        for (Book b : books) {
            if (b.getName().equals(name)) {
                book = b;
            }
        }
        if (book == null) {
            System.out.println("没有找到对应的图书");
            return;
        }
        book.setName(view.inputBookName());
        book.setInfo(view.inputBookInfo());
        book.setPrice(view.inputBookPrice());
        book.setPublishTime(view.inputBookPublishTime());
    }

    public void del(String name) {
        Book book = null;
        for (Book b : books) {
            if (b.getName().equals(name)) {
                book = b;
            }
        }
        books.remove(book);
        if (book == null) {
            System.out.println("没有找到对应的图书");
        } else {
            System.out.println("已删除以下图书：");
            System.out.println(book.toString());
        }
    }

    public void saveFile() {
        File file = new File("books.save");
        file.delete();
        try (ObjectOutputStream fileOutput = new ObjectOutputStream(new FileOutputStream(file))) {
            System.out.println("正在保存中");
            fileOutput.writeObject(Dao.books);
        } catch (IOException e) {
            System.out.println("保存失败");
        }
    }

    public void readFile() {
        File file = new File("books.save");
        if (file.isFile()) {
            try (ObjectInputStream fileInput = new ObjectInputStream(new FileInputStream(file))) {
                Dao.books = (HashSet<Book>) fileInput.readObject();
            } catch (Exception e) {
                System.out.println("读取失败");
            }
        }
    }
}
